package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.AnalysisCacheParam;
import com.hunttown.mes.rpc.domain.AnalysisCacheParamDTO;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2018-11-26 15:04:01
 */
public class AnalysisCacheParamConverts {

    private static Logger LOG = LoggerFactory.getLogger(AnalysisCacheParamConverts.class);

    public static List<AnalysisCacheParamDTO> convert(List<AnalysisCacheParam> analysisCacheParamList) {
        if (CollectionUtils.isEmpty(analysisCacheParamList)) {
            return Collections.EMPTY_LIST;
        }

        List<AnalysisCacheParamDTO> analysisCacheParamListResult = Lists.newArrayList();
        for (AnalysisCacheParam analysisCacheParam : analysisCacheParamList) {
            analysisCacheParamListResult.add(convert(analysisCacheParam));
        }
        return analysisCacheParamListResult;
    }

    public static AnalysisCacheParamDTO convert(AnalysisCacheParam analysisCacheParam) {
        try {
            return BeanUtils.copy(analysisCacheParam, AnalysisCacheParamDTO.class);
        } catch (Exception e) {
            LOG.error("AnalysisCacheParam到AnalysisCacheParamDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisCacheParam到AnalysisCacheParamDTO转换失败", e);
        }
    }

    public static AnalysisCacheParam convert(AnalysisCacheParamDTO analysisCacheParamDTO) {
        try {
            return BeanUtils.copy(analysisCacheParamDTO, AnalysisCacheParam.class);
        } catch (Exception e) {
            LOG.error("AnalysisCacheParamDTO到AnalysisCacheParam转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "AnalysisCacheParamDTO到AnalysisCacheParam转换失败", e);
        }
    }
}